package com.java_foundation.thread_demo.test_9;

/**
 * @program: java_foundation
 * @description: 举例加深对yeild方法的理解
 * @author: xiongbangwen <Email>xiongbangwen@163.com</Email>
 * @create: 2020-06-30 23:03
 **/
public class YieldDemo implements Runnable {
    public void run() {
        for (int i = 0; i < 5; i++) {
            if ((i % 5) == 0) {
                System.out.println(Thread.currentThread() + "---  yield cpu");
                //调用yield让出cpu执行权，放弃时间片，进行下一轮调度
                Thread.yield();
            }
        }

        System.out.println(Thread.currentThread() + "---  yield over");
    }

    public static void main(String[] args) {
        Thread d1 = new Thread(new YieldDemo());
        Thread d2 = new Thread(new YieldDemo());
        Thread d3 = new Thread(new YieldDemo());
        d1.start();
        d2.start();
        d3.start();
    }
}
